import datetime
import os
import tkinter.messagebox as message
import tkinter.ttk as ttk
from tkinter import *
from ScrapyFrame.myScrapy import getData
from ScrapyFrame.myScrapy import parseData

dataList = os.listdir('./data')
top = Tk()
titleList = Listbox(master=top)
v = Listbox(master=top)
com1 = ttk.Combobox(top)
com2 = ttk.Combobox(top)
com3 = ttk.Combobox(top)
com4 = ttk.Combobox(top)


def do_top(select):
    if len(select) == 0:
        message.showinfo('提示', '请选中日期进行统计')
        return
    parseData.do_top(dataList[select[0]])


def do_special(select):
    if len(select) == 0:
        message.showinfo('提示', '请选中日期进行统计')
        return
    parseData.do_special(dataList[select[0]])


def do_length(select):
    if len(select) == 0:
        message.showinfo('提示', '请选中日期进行统计')
        return
    parseData.do_length(dataList[select[0]])


def do_new(select):
    if len(select) == 0:
        message.showinfo('提示', '请选中日期进行统计')
        return
    dis = parseData.do_new(dataList[select[0]], dataList[select[0] - 1])

    v.delete(0)
    for item in dis:
        v.insert('end', item)


def refresh_list():
    global dataList
    dataList = os.listdir('./data')
    titleList.delete(0, 'end')
    for e in dataList:
        titleList.insert("end", e)
    titleList.place(x=10, y=250)


def get_data():
    getData.do()
    refresh_list()
    refresh_com()


def view(select):
    if len(select) == 0:
        message.showinfo('提示', '请选中日期进行统计')
        return
    v.delete(0)
    with open('./data/' + dataList[select[0]] + '/count.txt', 'r', encoding='utf-8') as fp:
        lines = fp.readlines()
        for line in lines:
            v.insert('end', line)


def delete_list(select):
    if len(select) == 0:
        message.showinfo('提示', '请选中日期进行统计')
        return
    v.delete(0)
    os.remove('./data/' + dataList[select[0]] + '/count.txt')
    os.rmdir('./data/' + dataList[select[0]])
    refresh_list()


def refresh_com():
    list_data = os.listdir('./data')
    com1['value'] = list_data
    com2['value'] = list_data
    com3['value'] = list_data
    com4['value'] = list_data


def do_compare_top(start1, end1, start2, end2):
    parseData.do_compare_top(start1, end1, start2, end2)


def do_compare_special(start1, end1, start2, end2):
    parseData.do_compare_special(start1, end1, start2, end2)


def do_compare_length(start1, end1, start2, end2):
    parseData.do_compare_length(start1, end1, start2, end2)


def do_compare_new(start1, end1, start2, end2):
    dis = parseData.do_compare_new(start1, end1, start2, end2)
    v.delete(0)
    for di in dis:
        v.insert('end', di)


if __name__ == '__main__':
    top.title("China Daily 标题分析系统")
    top.geometry('1000x600+%d+%d' % ((top.winfo_screenwidth() - 1000) / 2, (top.winfo_screenheight() - 600) / 2))
    top.resizable(False, False)

    timeLabel = Label(text='日期: ' + str(datetime.datetime.now().strftime('%y-%m-%d')))
    timeLabel.place(x=700, y=10)

    Label(text='-' * 200).place(x=0, y=50)

    tip = Label(text='')

    Label(text='-' * 200).place(x=0, y=200)

    v.place(x=300, y=250)

    refresh_list()
    Label(text='数据列表').place(x=10, y=220)
    Label(text='对比分析').place(x=500, y=220)
    # 数据获取
    Button(text="爬取当天数据", command=lambda: get_data()).place(x=10, y=70)
    # 统计分析
    Button(text="高频词汇统计", command=lambda: do_top(titleList.curselection())).place(x=10, y=440)
    Button(text="词汇词性统计", command=lambda: do_special(titleList.curselection())).place(x=10, y=475)
    Button(text="词汇长度统计", command=lambda: do_length(titleList.curselection())).place(x=10, y=510)
    Button(text="新关键字统计", command=lambda: do_new(titleList.curselection())).place(x=10, y=545)

    # 对比分析
    Label(text='样本一').place(x=500, y=250)
    Label(text='样本二').place(x=500, y=300)
    Label(text='至').place(x=720, y=250)
    Label(text='至').place(x=720, y=300)

    com1['value'] = (1, 2, 3, 4, 5, 6)
    com1.place(x=550, y=250)

    com2['value'] = (1, 2, 3, 4, 5, 6)
    com2.place(x=750, y=250)

    com3['value'] = (1, 2, 3, 4, 5, 6)
    com3.place(x=550, y=300)

    com4['value'] = (1, 2, 3, 4, 5, 6)
    com4.place(x=750, y=300)

    refresh_com()

    Button(text="高频词汇对比", command=lambda: do_compare_top(com1.get(), com2.get(), com3.get(), com4.get())).place(x=500,
                                                                                                                y=350)
    Button(text="词汇词性对比", command=lambda: do_compare_special(com1.get(), com2.get(), com3.get(), com4.get())).place(
        x=600, y=350)
    Button(text="词汇长度对比", command=lambda: do_compare_length(com1.get(), com2.get(), com3.get(), com4.get())).place(
        x=500, y=400)
    Button(text="新关键字对比", command=lambda: do_compare_new(com1.get(), com2.get(), com3.get(), com4.get())).place(x=600,
                                                                                                                y=400)
    Button(text="刷新", command=lambda: refresh_list()).place(x=170, y=250)
    Button(text="查看", command=lambda: view(titleList.curselection())).place(x=170, y=300)
    Button(text="删除", command=lambda: delete_list(titleList.curselection())).place(x=170, y=350)
    Button(text="词云", command=lambda: delete_list(titleList.curselection())).place(x=170, y=400)

    # 监听
    top.mainloop()
